import re
import math
import itertools
import random

w = open('city_trig.txt', 'r')

t = w.read()

w.close()

city_names = ["City1", "City2", "City3", "City4", "City5", "City6"]

def permutations(items, choice, string = [], l = []):
    if len(items) == 1:
        l.append(string + items)
    elif len(items)> 1:
        new_items = [item for item in items if item != choice]
        for item in new_items:
            permutations(new_items, item, string + [choice], l)

def binstring_2_list(binstring):
    l = []
    for x in binstring:
        if x == "0":
            l.append("not set")
        else:
            l.append("set")
    return l

def city_macro():
    perms = []
    for city in city_names:
        permutations(city_names, city, [], perms)
    
    rand_smpl = [ perms[i] for i in sorted(random.sample(xrange(len(perms)), 304))]
    perms2 = perms + rand_smpl
    s = ["".join(seq) for seq in itertools.product("01", repeat=10)]
    z = [binstring_2_list(x) for x in s]
    total = 0
    w = open("city_trigs.txt", 'w')
    for seq in perms2:
        nt = t
        p = re.compile(r'(?<="Switch1", )(not set|set)')
        nt = p.sub(z[total][0], nt)
        p = re.compile(r'(?<="Switch2", )(not set|set)')
        nt = p.sub(z[total][1], nt)
        p = re.compile(r'(?<="Switch3", )(not set|set)')
        nt = p.sub(z[total][2], nt)
        p = re.compile(r'(?<="Switch4", )(not set|set)')
        nt = p.sub(z[total][3], nt)
        p = re.compile(r'(?<="Switch5", )(not set|set)')
        nt = p.sub(z[total][4], nt)
        p = re.compile(r'(?<="Switch6", )(not set|set)')
        nt = p.sub(z[total][5], nt)
        p = re.compile(r'(?<="Switch7", )(not set|set)')
        nt = p.sub(z[total][6], nt)
        p = re.compile(r'(?<="Switch8", )(not set|set)')
        nt = p.sub(z[total][7], nt)
        p = re.compile(r'(?<="Switch9", )(not set|set)')
        nt = p.sub(z[total][8], nt)
        p = re.compile(r'(?<="Switch10", )(not set|set)')
        nt = p.sub(z[total][9], nt)
        p = re.compile(r'(?<="Player 1", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[0], nt)
        p = re.compile(r'(?<="Player 2", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[1], nt)
        p = re.compile(r'(?<="Player 3", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[2], nt)
        p = re.compile(r'(?<="Player 4", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[3], nt)
        p = re.compile(r'(?<="Player 5", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[4], nt)
        p = re.compile(r'(?<="Player 6", "Buildings", All, ")City[0-9](?=")')
        nt = p.sub(seq[5], nt)
        p = re.compile(r'Player Initial City Choice 1')
        nt = p.sub("Player Initial City Choice " + str(total), nt)
        p = re.compile(r'Exactly, 0')
        nt = p.sub("Exactly, " + str(total), nt)
        print>>w, nt
        total += 1
    w.close()

    
    
